<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      * {
        padding: 0;
        margin: 0;
        list-style: none;
        box-sizing: border-box;
      }

      .box {
        width: 300px;
        height: 200px;
        border: 1px solid #000;
        margin: auto;
      }

      .box > .head {
        display: flex;
        justify-content: space-between;
      }

      .box > .head > li {
        flex-grow: 1;
        text-align: center;
        cursor: pointer;
      }

      .active {
        background-color: #87ceeb;
      }

      .main {
        position: relative;
      }

      .main > li {
        position: absolute;
        top: 0;
        left: 0;
        width: 100%;
        height: 178px;
        background-color: #87ceeb;
      }

      .mainActive {
        z-index: 999;
      }
    </style>
  </head>
  <body>
    <div class="box">
      <ul class="head">
        <li class="active">标题一</li>
        <li>标题二</li>
        <li>标题三</li>
      </ul>

      <ul class="main">
        <li class="mainActive">内容一</li>
        <li>内容二</li>
        <li>内容三</li>
      </ul>

      <script>
        let title = document.querySelector(".head");
        let li_list = title.querySelectorAll("li");
        // console.log(li_list);
        let content = document.querySelector(".main");
        let lis = content.querySelectorAll("li");
        console.log(lis);
        for (let i = 0; i < li_list.length; i++) {
          li_list[i].setAttribute("index", i);
          li_list[i].onclick = function () {
            for (let i = 0; i < li_list.length; i++) {
              li_list[i].className = "";
            }
            this.className = "active";
            let index = this.getAttribute("index");

            for (let i = 0; i < li_list.length; i++) {
              lis[i].className = "";
            }
            lis[i].className += " mainActive";
          };
        }
      </script>
    </div>
  </body>
</html>
